package Controllers.GenericToolsForController;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;

import javax.swing.ImageIcon;

import Boundry.Splash;
import Entity.Report;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.sun.media.sound.Toolkit;

/**
 * PDF files are read to this class and sent with it.
 * Needed in order to send serializable object.
 *
 */
public class PDFfile extends Document implements Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        public byte[] input;
        private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
  		      Font.BOLD);
  		  private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
  		      Font.NORMAL, BaseColor.RED);
  		  private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
  		      Font.BOLD);
  		  private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
  		      Font.BOLD);
  		  

  		  
  		 public PDFfile() {}
  		 
  		 
  		 
        public PDFfile(File file) throws IOException
        {
        	input=Files.readAllBytes(file.toPath());
            System.out.println("The file Size : "+ file.length()  +" The btye array size " + input.length);
        }
        
        public boolean read(String fileName)
        {
    		File f = new File(fileName);
        	try {
        		input = new byte[ (int) f.length()];
				input= Files.readAllBytes(f.toPath());
				
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
        	//f.delete();
            System.out.println("The file Size : "+ f.length()  +" The btye array size " + input.length);
            return true;
        }
  
        public boolean write(String fileName)
        {
            File newfile = new File(fileName);
		try {
			FileOutputStream fos = new FileOutputStream(newfile);
			fos.write(input);
			fos.close();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}
  return true;
}
        public PDFfile(String file) throws IOException
        {
        	input=Files.readAllBytes(new File(file).toPath());
            System.out.println("The file Size : "+ file.length()  +" The btye array size " + input.length);
        }
        
        public PDFfile(Report report, String filepath)
        {
            System.out.println("before  PdfWriter.getInstance" );
        	try {
        		      PdfWriter.getInstance(this, new FileOutputStream(filepath));
        		      this.open();
        		    //TODO Ron add logo
        				this.add(new Paragraph(report.getType().toString()+ " REPORT:"));
        		      addMetaData(report.getType().toString(),"Automated report");
        		      addContent(report.getFile());
        		      this.close();
        		    } catch (Exception e) {
        		      e.printStackTrace();
        		    }
        	try {
				input=Files.readAllBytes(new File(filepath).toPath());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
            System.out.println("after  PdfWriter.getInstance" );

        	
        	
        }
        

        // iText allows to add metadata to the PDF which can be viewed in your Adobe
        // Reader
        // under File -> Properties
        private  void addMetaData(String strMetadataTitle, String subject) {
          this.addTitle(strMetadataTitle);
          this.addSubject(subject);
          this.addAuthor("ICM System Automate generation");
          this.addCreator("ICM System Automate generation");
          
        }

        private  void addTitlePage(String pageTitle, String peragraphTitle)
            throws DocumentException {
          Paragraph preface = new Paragraph();
          // We add one empty line
          addEmptyLine(preface, 1);
          // Lets write a big header
          preface.add(new Paragraph(pageTitle, catFont));

          addEmptyLine(preface, 1);
          // Will create: Report generated by: _name, _date
          preface.add(new Paragraph(peragraphTitle, 
              smallBold));
          addEmptyLine(preface, 3);
          //preface.add(new Paragraph("This document describes something which is very important ",
         //     smallBold));

          addEmptyLine(preface, 8);

          this.add(preface);
          // Start a new page
          this.newPage();
        }

        private  void addContent(File originalfile) throws DocumentException {
      	Anchor anchor = new Anchor("ICM System filesystem:", catFont);
          anchor.setName(" ");

          Chapter catPart = new Chapter(new Paragraph(anchor), 1);
      	BufferedReader reader = null;
      	try
      	{
      		if (originalfile != null)
      		{
      			reader = new BufferedReader( new FileReader( originalfile));
      			while (reader.ready())
      			{
      				this.add(new Paragraph(reader.readLine()));
      			}
      			        
      		}
      		
      	}
      	catch ( IOException e)
      	{
      	}
      	finally
      	{
      		try
      		{
      			if ( reader != null)
      				reader.close( );
      		}
      		catch ( IOException e)
      		{
      		}
           }

        }



        private static void addEmptyLine(Paragraph paragraph, int number) {
          for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
          }
        }
		
	   

        

}